package jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * -Dcom.sun.management.jmxremote.port=8288 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.201.227
 * Created by sunyuming on 19/1/25.
 */
public class HelloAgent2 {
    public static void main(String[] args) throws Exception {

        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("jmxBean:name=hello");
        server.registerMBean(new Hello(), objectName);

        /**
         * 方式二 代码注册
         * 但启动时仍然需要指定 -Djava.rmi.server.hostname=192.168.57.234，否则客户端jconsole无法连接
         */
        Registry registry = LocateRegistry.createRegistry(8288);
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8288/jmxrmi");
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, server);
        cs.start();
    }
}
